$(function () {
    $('#btnAdd').click(function () {
        window.location.href = `/crud/createoredit`;
    })
    $('#btnCancle').click(function () {
        window.location.href = `/crud/indexview`
    })
    $('#btnSave').click(function () {
        let id = $('[name=id]').val()
        let name = $('[name=name]').val()
        let supplier = $('[name=supplier]').val()
        let price = $('[name=price]').val()
        let stock = $('[name=stock]').val()
        let remarks = $('[name=remarks]').val()
        let obj = {
            id, name, supplier, price, stock, remarks
        }
        $.post('/crud/save', obj, function () {
            window.location.href = `/crud/indexview`;
        })
    })
    $('#btnQuery').click(function () {
        let keyword = $("[name=keyword]").val();
        let obj = {
            keyword
        }
        $.post('/crud/save', obj, function (res) {
            console.log(res)
            $('.tbRow').html('')
            res.forEach(item => {
                let html = `
                <tr class="tbRow">
                <td>${item.id}</td>
                <td>${itiem.productName}</td>
                <td>${item.supplier}</td>
                <td>${item.price}</td>
                <td>${item.stock}</td>
                <td>${item.remarks}</td>
                <td>
                    <input type="button" value="编辑" onclick="btnEdit(${item.id})">
                        <input type="button" value="删除" onclick="btnDel(${item.id})">
                        </td>
                    </tr>
                `
                $('#tbData').append(html)
            });
        })
    })
})
function btnEdit(id) {
    window.location.href = `/crud/createoredit/${id}`
}
function btnDel(id) {
    let msg = "确认要删除吗？";
    if (confirm(msg)) {
        window.location.href = `/crud/deleted/${id}`
    }
}